package com.wangxiang.sys.manage.service.Impl;

import com.wangxiang.sys.manage.dao.RoleDao;
import com.wangxiang.sys.manage.dao.UserDao;
import com.wangxiang.sys.manage.entity.RoleEntity;
import com.wangxiang.sys.manage.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.Query;

import com.wangxiang.sys.manage.dao.RoleUserDao;
import com.wangxiang.sys.manage.entity.RoleUserEntity;
import com.wangxiang.sys.manage.service.RoleUserService;


@Service("roleUserService")
public class RoleUserServiceImpl extends ServiceImpl<RoleUserDao, RoleUserEntity> implements RoleUserService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleUserDao roleUserDao;
    @Autowired
    private UserDao userDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RoleUserEntity> page = this.page(
                new Query<RoleUserEntity>().getPage(params),
                new QueryWrapper<RoleUserEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过角色id查询连个表的组合数句
     * @param roleId
     * @return
     */
    @Override
    public HashMap<String, String> getRelEntity(int roleId) {

        //查询，角色信息
        if (roleId!=0){
            RoleEntity roleEntity = roleDao.selectById(roleId);
            //关系表
            RoleUserEntity roleUserEntity = roleUserDao.selectById(roleId);
            //管理员用户表
            UserEntity userEntity = userDao.selectById(roleUserEntity.getUserId());

            //组合数据
            HashMap<String, String> map = new HashMap<>();
            map.put("applyName",userEntity.getLoginId());
            map.put("applyId",String.valueOf(userEntity.getUserId()));
            map.put("roleName",roleEntity.getName());
            return map;
        }
        return null;
    }



}